﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Minho.FluentCaptcha;
using System.Drawing;
using YMJF_Service.Common;
using System.Runtime.Caching;

namespace YMJF_Service.Controllers
{
    /// <summary>
    /// 生成验证码
    /// </summary>
   
    public class FluentCaptchaController : Controller
    {
        MemoryCacher mc = new MemoryCacher();
      
        public ActionResult FluentCaptcha()
        {
            CaptchaOptions options = new CaptchaOptions
            {
                GaussianDeviation = 0,
                FontFamily = new CaptchaFontFamily(new List<string>{ "arial"}),
                Colors = new CaptchaColor(new List<Color> {Color.FromArgb(0, 132,35) })
            };
            //mc.Contains()
            MemoryStream stream = new MemoryStream();
            using (ICapatcha capatch = new Minho.FluentCaptcha.FluentCaptcha())
            {
                capatch.Options = options;
               // CaptchaResult captchaResult = capatch.DrawBackgroud().DrawLine().DrawText().Atomized().DrawBroder().DrawImage();
                CaptchaResult captchaResult = capatch.DrawText().DrawImage();
                var policy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromMinutes(1) };//设置验证码1分钟内有效
                mc.Add(captchaResult.Text,"vcode",policy);//保存用户token
                using (captchaResult)
                {
                    captchaResult.Bitmap.Save(stream, ImageFormat.Gif);
                    
                }
            }
            return File(stream.ToArray(), "image/gif");
        } 
    }
}
